# 项目：cxl
# 文件：0520
# 作者：CXL
# 日期：2025/5/2016:07
# coding:utf-8


import random
from selenium import webdriver
from selenium.webdriver.support.ui import Select
import time


def get_driver():
    driver = webdriver.Edge()
    driver.implicitly_wait(5)  # 隐式等待5秒
    driver.maximize_window()  # 最大化浏览器窗口
    return driver


def input(element, value):
    element.click()  # 点击元素
    element.clear()  # 清空输入框
    element.send_keys(value)  # 输入指定值


def select_random_first(element):
    options_count = len(Select(element).options)  # 获取下拉选项总数
    random_index = random.randint(1, options_count - 1)  # 随机选择索引（跳过第一个选项，可能是占位符）
    Select(element).select_by_index(random_index)  # 按索引选择


if __name__ == '__main__':
    driver = get_driver()
    driver.get('http://localhost:8080/YunDuanBoss/login/')
    uname = driver.find_element('name', 'userName')
    input(uname, 'YDHR000')
    upass = driver.find_element('name', 'userPass')
    input(upass, 'yunduan123')
    driver.find_element('css selector', '#form-login > div > div > div.modal-footer > button').click()
    # 初始化浏览器并打开页面
    driver = get_driver()
    driver.get('http://localhost:8080/YunDuanBoss/employeeAttendance')  # 假设这是基础URL
    # 测试导航切换按钮（移动端视图）
    try:
        nav_toggle = driver.find_element(by='css selector',
                                         value='.navbar-toggle.navbar-ce')
        nav_toggle.click()
        print("成功点击导航切换按钮")
        time.sleep(1)
    except Exception as e:
        print(f"导航切换交互失败: {str(e)}")

    # 测试导航菜单项（需要页面实际选择器）
    try:
        # 示例 - 需要实际的菜单项选择器
        # menu_item = driver.find_element(by='css selector', value='.nav-list a:first-child')
        # menu_item.click()
        print("此处需提供正确的选择器来测试导航菜单项")
    except Exception as e:
        print(f"导航菜单测试跳过 - 需要提供正确的选择器: {str(e)}")

    # 测试模态对话框（密码修改、二级密码、问题反馈）
    modals_to_test = ['form-password', 'secondPass-modal', 'form-problem']

    for modal_id in modals_to_test:
        try:
            # 此处需要实际触发模态框的按钮
            # trigger_button = driver.find_element(by='css selector', value=f'[data-target="#{modal_id}"]')
            # trigger_button.click()
            # time.sleep(1)

            # 验证模态框是否可见
            modal = driver.find_element(by='id', value=modal_id)
            if modal.is_displayed():
                print(f"成功打开模态框 {modal_id}")
                # 此处可添加表单交互测试
            else:
                print(f"模态框 {modal_id} 不可见 - 可能需要触发元素")
        except Exception as e:
            print(f"测试模态框 {modal_id} 失败: {str(e)}")

    # 测试日期时间选择器功能
    try:
        datetime_pickers = driver.find_elements(by='css selector',
                                                value='.datetimepicker')
        print(f"找到 {len(datetime_pickers)} 个日期时间选择器元素")
    except Exception as e:
        print(f"日期时间选择器测试失败: {str(e)}")

    # 清理
    driver.quit()
    print("冒烟测试完成")